﻿@model Age.Qna.Site.Mvc.Models.QnaEntity

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/age.ui.qna.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/age.ui.qna.model.js")" type="text/javascript"></script>

@if (false)
{
<script src="../../Scripts/jquery-1.5.1-vsdoc.js" type="text/javascript"></script>
<script src="../../Scripts/age.ui.qna.js" type="text/javascript"></script>
<script src="../../Scripts/age.ui.qna.model.js" type="text/javascript"></script>
}

<script type="text/javascript">

	$(function () {

		$('form').submit(function () {

			// Instantiate the model
			var entity = QnaModel.QnaEntity;

			// Bind to QNA data
			// Get categories and assign
			entity.QuestionData.CategoryID = [4, 5, 7, 9, 10, 23, 14, 15];

			// Finally bind
			$(this).bindQnaForm({ containerId: 'qnaContainer', model: entity, debug: false });

			if ($(this).valid()) {
				$.ajax({
					url: "/Question/Save",
					type: "POST",
					data: { data: JSON.stringify(entity) },
					success: function (result) {
						$('#result').html(result);
						$('#qnaContainer').html('');
						// Create a link to create a new question.
						var s = $('#result span:contains("create a new question")');
						var v = s.text();
						s.html('');
						var t = document.createElement('a');
						$(t).attr('href', '/Question/New')
								.html(v)
								.appendTo(s);

					},
					error: function (d) {
						alert('fail');
					}
				});
			}
			return false;
		});

		$('#editquestion').click(function () {
			if ($('.qEditElement').is(':visible')) {
				$('.qEditElement').slideUp('fast');
			} else {
				$('.qEditElement').slideDown('fast');
			}
		});

		$('#addanswerchoice').click(function () {
			$(this).addChoice('qAnswerPanel');
		});

		// Load on start:
		$(this).addQuestion('qQuestionPanel');
		$(this).addChoice('qAnswerPanel');

	});

</script>

<h2>Create a Question</h2>

@using (Html.BeginForm())
{
<div id="result"></div>
<div id="qnaContainer">
	<div id="qQuestionPanel"></div>
	<div id="qAnswerPanel"></div>
	<div id="qControlPanel">
		<input type="button" id="addanswerchoice" name="addanswerchoice" value="Add answer" class="qbutton" />
		<input type="submit" id="savequestion" name="savequestion" value="Save" class="qbutton" />
		<input type="button" id="editquestion" name="editquestion" value="Edit" class="qbutton" />
	</div>
</div>
}
